package com.uh.calculation.operator;

import com.uh.calculation.bean.FormulaBean;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.TbFeild;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.util.FormulaUtil;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.TimeUtil;
import com.uh.microservice.core.dao.JdbcDao;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 本月M12+上月M13)/本月M13  单表  上月M13字典 加#m
 */
@Data
public class SyOperator {
    private JdbcDao jdbcDao;
    private WarnJiSuanBean warnJiSuanBean;


    public SyOperator(JdbcDao jdbcDao, WarnJiSuanBean warnJiSuanBean) {
        this.jdbcDao = jdbcDao;
        this.warnJiSuanBean = warnJiSuanBean;

    }

    /**
     * 单表数据
     *
     * @param list
     * @return
     */
    public void jisuan(List<Map<String, Object>> ls, List<OperatorVal> list) {
        if (ls == null || ls.size() == 0) {
            return;
        }
        this.warnJiSuanBean = warnJiSuanBean;
        Date lastTime = warnJiSuanBean.getLastTime();
        //反转2021 与 2022字段区别
        //GetFileds getFileds = new GetFileds(jdbcDao,list,warnJiSuanBean);
        for (OperatorVal operatorVal : list) {
            try {
                List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
                String field = operatorVal.getField();
                List<TbFeild> tbFeilds = GetBean.getTbf(field);
                String f1 = tbFeilds.get(0).getFieldName();
                String f2 = tbFeilds.get(1).getFieldName();
                String f3 = tbFeilds.get(2).getFieldName();
                for (int i = 1; i < ls.size(); i++) {
                    try {
                        Map m = ls.get(i);
                        //时间判断
                        String timeCode = m.get("time_code").toString();
                        //时间游标判断，及核查规则是否符合
                        if (TimeUtil.bj(lastTime, timeCode, warnJiSuanBean.getYear_is_month(), warnJiSuanBean.getIs_hc())) {
                            String d1 = ls.get(i).get(f1).toString();
                            String d2 = ls.get(i - 1).get(f2).toString();
                            String d3 = ls.get(i).get(f3).toString();
                            //根据当前计算类解析规则  将数据和字段对应
                            tbFeilds.get(0).setData(d1);
                            tbFeilds.get(1).setData(d2);
                            tbFeilds.get(2).setData(d3);
                            //扔进公式解析工具
                            new FormulaUtil(tbFeilds, formulaBeans, warnJiSuanBean, operatorVal.getDimension(), timeCode);
                        }
                    } catch (Exception e) {

                    }
                }
            } catch (Exception e) {

            }


        }
    }
}
